package com.kitty.game.achieve.service;

import com.kitty.common.cache.RedisService;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.achieve.body.*;
import com.kitty.game.achieve.message.*;
import com.kitty.game.common.BoosTitleDeal;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.player.model.BossImgInfos;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AchieveConfigService {
    @Autowired
    RoleService roleService;
    @Autowired
    RedisService redisService;
    //查看boss图鉴
    public void queryAchieveBossImg(IoSession session){
        RespBossImgList respBossImgList = queryBossImgList(session);
        MessagePusher.pushMessage(session, respBossImgList);
    }
    //激活boss图鉴
    public void jiHuoBossImg(IoSession session,ReqJiHuoBossImg reqJiHuoBossImg){
        Role role = SessionUtils.getRoleBySession(session);
        BossImgInfos bossImgInfos = role.getExtendBox().getBossImgInfos();
        NutMap bossImgMap = bossImgInfos.getBossImgMap();
        int anInt = bossImgMap.getInt(reqJiHuoBossImg.getImgId()+"",1);
        int magPower = Integer.parseInt(DataCache.REDIS_DATA.get("tujian_mag"));
        int phyPower = Integer.parseInt(DataCache.REDIS_DATA.get("tujian_phy"));
        int life = Integer.parseInt(DataCache.REDIS_DATA.get("tujian_life"));
        if(anInt==2){
            bossImgInfos.setMagPower(bossImgInfos.getMagPower()+magPower);
            bossImgInfos.setPhyPower(bossImgInfos.getPhyPower()+phyPower);
            bossImgInfos.setMaxLife(bossImgInfos.getMaxLife()+life);
            bossImgMap.put(reqJiHuoBossImg.getImgId()+"",3);
            RespJiHuoBossImgSucc respJiHuoBossImgSucc = new RespJiHuoBossImgSucc();
            respJiHuoBossImgSucc.setImgId((short)reqJiHuoBossImg.getImgId());
            MessagePusher.pushMessage(role, respJiHuoBossImgSucc);
            SpringUtils.getRoleService().sendExtraAttribute(role);
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("激活成功！"));
            int i = 0;
            for (Map.Entry<String, Object> stringObjectEntry : bossImgMap.entrySet()) {
                int state = Integer.parseInt(stringObjectEntry.getValue().toString());
                if(3 == state){
                    i+=1;
                }
            }
            if(i>=1){
                SpringUtils.getBean(RoleService.class).addAchieveScore(role,"108003");
            }
            if(i>=5){
                SpringUtils.getBean(RoleService.class).addAchieveScore(role,"108001");
            }
            if(i>=10){
                SpringUtils.getBean(RoleService.class).addAchieveScore(role,"108002");
            }
            if(i>=20){
                SpringUtils.getBean(RoleService.class).addAchieveScore(role,"108004");
            }
        }else{
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("当前图签暂未获得，无法激活！"));
        }

    }
    //查询首杀
    public void queryFirstBossInfo(IoSession session){
        Role role = SessionUtils.getRoleBySession(session);
        //超级boss
        RespSupetBossFirst supetBossFirst = querySuperBossFirst();
        MessagePusher.pushMessage(role, supetBossFirst);

        //九天
        RespJiuTianBossFirst jiuTianBossFirst = queryJiuTianBossFirst();
        MessagePusher.pushMessage(role, jiuTianBossFirst);

        //魔龙
        RespMoLongBossFirst respMoLongBossFirst =  queryMoLongBossFirst();
        MessagePusher.pushMessage(role, respMoLongBossFirst);
        //七杀
        RespQiShaBossFirst qiShaBossFirst = queryQiShaBossFirst();
        if(qiShaBossFirst != null){
            MessagePusher.pushMessage(role, qiShaBossFirst);
        }

    }
    public RespMoLongBossFirst queryMoLongBossFirst(){
        RespSupetBossFirst superBossFirst = new RespSupetBossFirst();
        getBossTitle("mlzs",superBossFirst);
        getBossTitle("mltt",superBossFirst);
        RespMoLongBossFirst respMoLongBossFirst  = new RespMoLongBossFirst();
        respMoLongBossFirst.setSupetBossTitleList(superBossFirst.getSupetBossTitleList());
        return respMoLongBossFirst;
    }



    public RespQiShaBossFirst queryQiShaBossFirst(){
        RespSupetBossFirst superBossFirst = new RespSupetBossFirst();
        getBossTitle("qisha",superBossFirst);
        if(superBossFirst ==null || superBossFirst.getSupetBossTitleList() == null){
            return null;
        }
        if(superBossFirst.getSupetBossTitleList().size()>0){
            RespQiShaBossFirst respQiShaBossFirst  = new RespQiShaBossFirst();
            List<QiShaBossTitle> qiShaBossTitles = new ArrayList<>();
            QiShaBossTitle qiShaBossTitle = new QiShaBossTitle();
            qiShaBossTitle.setKillTime(superBossFirst.getSupetBossTitleList().get(0).getKillTime());
            qiShaBossTitle.setSuperBossKillRoleList(superBossFirst.getSupetBossTitleList().get(0).getSuperBossKillRoleList());
            qiShaBossTitles.add(qiShaBossTitle);
            respQiShaBossFirst.setQiShaBossTitleList(qiShaBossTitles);
            return respQiShaBossFirst;
        }
        return null;
    }



    public RespJiuTianBossFirst queryJiuTianBossFirst(){
        RespSupetBossFirst superBossFirst = new RespSupetBossFirst();
        getBossTitle("jiutian_zhu",superBossFirst);
        getBossTitle("jiutian_cheng",superBossFirst);
        getBossTitle("jiutian_you",superBossFirst);
        getBossTitle("jiutian_xuan",superBossFirst);

        getBossTitle("jiutian_bian",superBossFirst);
        getBossTitle("jiutian_jun",superBossFirst);
        RespJiuTianBossFirst respJiuTianBossFirst  = new RespJiuTianBossFirst();
        respJiuTianBossFirst.setSupetBossTitleList(superBossFirst.getSupetBossTitleList());
        return respJiuTianBossFirst;
    }


    public RespSupetBossFirst querySuperBossFirst(){
        RespSupetBossFirst superBossFirst = new RespSupetBossFirst();
        superBossFirst.setFlag((byte)1);
        getBossTitle("hxyh",superBossFirst);
        getBossTitle("xlmz",superBossFirst);
        getBossTitle("myxh",superBossFirst);
        getBossTitle("cxgy",superBossFirst);

        getBossTitle("jydd",superBossFirst);
        getBossTitle("zwdd",superBossFirst);
        getBossTitle("xldd",superBossFirst);
        getBossTitle("sldd",superBossFirst);
        return superBossFirst;
    }
    public void getBossTitle(String type,RespSupetBossFirst superBossFirst){
        List<SupetBossTitle> supetBossTitleList = superBossFirst.getSupetBossTitleList();
        if(supetBossTitleList==null){
            supetBossTitleList = new ArrayList<>();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SupetBossTitle supetBossTitle = new SupetBossTitle();
        String bossName = getBossNameByTitle(type);
        supetBossTitle.setBossName(bossName);
        List<SuperBossKillRole> list= new ArrayList<>();
        List<Map<String, String>> KillList = SpringUtils.getBean(BoosTitleDeal.class).getBossTitleInfo(type);
        for (Map<String, String> stringStringMap : KillList) {
            String time = stringStringMap.get("time");
            Date parse = null;
            try {
                parse = sdf.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            supetBossTitle.setKillTime(parse.getTime());
            SuperBossKillRole superBossKillRole = new SuperBossKillRole();
            Role role = roleService.getPlayerBy(stringStringMap.get("name"));
            superBossKillRole.setGid(role.getUid()+"");
            superBossKillRole.setIcon(role.getRoleIcon());
            superBossKillRole.setLevel(role.getLevel());
            superBossKillRole.setName(stringStringMap.get("name"));
            list.add(superBossKillRole);
        }
        if(list.size()>0){
            supetBossTitle.setSuperBossKillRoleList(list);
            supetBossTitleList.add(supetBossTitle);
            superBossFirst.setSupetBossTitleList(supetBossTitleList);
        }
    }
    private String getBossNameByTitle(String type){
        String bossName = "";
        switch (type){
            case "hxyh":
                bossName="黑熊妖皇";
                break;
            case "xlmz":
                bossName="血炼魔猪";
                break;
            case "myxh":
                bossName="魅影蝎后";
                break;
            case "cxgy":
                bossName="赤血鬼猿";
                break;
            case "zyyh":
                bossName="紫衣妖凰";
                break;
            case "sldd":
                bossName="杀戮魔皇";
                break;
            case "jydd":
                bossName="九幽冥雀";
                break;
            case "zwdd":
                bossName="真武魔帝";
                break;
            case "xldd":
                bossName="东山邪灵";
                break;
            case "jiutian_hao":
                bossName="昊天君";
                break;
            case "jiutian_yang":
                bossName="阳天君";
                break;
            case "jiutian_chi":
                bossName="赤天君";
                break;
            case "jiutian_zhu":
                bossName="朱天君";
                break;
            case "jiutian_cheng":
                bossName="成天君";
                break;
            case "jiutian_you":
                bossName="幽天君";
                break;
            case "jiutian_xuan":
                bossName="玄天君";
                break;
            case "jiutian_bian":
                bossName="变天君";
                break;
            case "jiutian_jun":
                bossName="钧天君";
                break;
            case "qisha":
                bossName="七杀星君";
                break;
        }
        return bossName;
    }
    public  RespBossImgList  queryBossImgList(IoSession session){
        Role role = SessionUtils.getRoleBySession(session);
        RespBossImgList respBossImgList = new RespBossImgList();
        List<BossImgInfo> sumList = new ArrayList<>();
        sumList.add(new BossImgInfo("mag_power", role.getExtendBox().getBossImgInfos().getMagPower()));
        sumList.add(new BossImgInfo("phy_power", role.getExtendBox().getBossImgInfos().getPhyPower()));
        sumList.add(new BossImgInfo("max_life", role.getExtendBox().getBossImgInfos().getMaxLife()));
        respBossImgList.setSumRoleList(sumList);
        List<BossImgInfo> sumPetList = new ArrayList<>();
        sumPetList.add(new BossImgInfo("mag_power", role.getExtendBox().getBossImgInfos().getMagPower()));
        sumPetList.add(new BossImgInfo("phy_power", role.getExtendBox().getBossImgInfos().getPhyPower()));
        sumPetList.add(new BossImgInfo("max_life", role.getExtendBox().getBossImgInfos().getMaxLife()));
        respBossImgList.setSumPetList(sumPetList);
        respBossImgList.setType((byte)2);
        int magPower = Integer.parseInt(DataCache.REDIS_DATA.get("tujian_mag"));
        int phyPower = Integer.parseInt(DataCache.REDIS_DATA.get("tujian_phy"));
        int life = Integer.parseInt(DataCache.REDIS_DATA.get("tujian_life"));
        List<BossImgInfo> currList = new ArrayList<>();
        currList.add(new BossImgInfo("mag_power", magPower));
        currList.add(new BossImgInfo("phy_power", phyPower));
        currList.add(new BossImgInfo("max_life", life));

        List<BossImgList> bossImgLists = new ArrayList<>();
        respBossImgList.setCurrList(currList);

        for (int i = 1; i <= 32; i++) {
            BossImgList bossImgList  = new BossImgList();
            bossImgList.setImgId((short)i);
            int bossState = BossImgType.getBossState(i+"");
            int anInt = role.getExtendBox().getBossImgInfos().getBossImgMap().getInt(i+"",1);
            bossImgList.setState((byte)anInt);
//            if(bossState>0){
//
//            }else{
//                bossImgList.setState((byte)1);
//            }

            bossImgLists.add(bossImgList);
        }
        respBossImgList.setBossImgLists(bossImgLists);
        return respBossImgList;
    }
}
